#import "cocos2d.h"
#import "OpenFeint/OpenFeint.h"
#import "SimpleAudioEngine.h"
#import "tapburstAppDelegate.h"
#import "RootViewController.h"
#import "LayerLogo.h"
#import "LayerMain.h"

@implementation tapburstAppDelegate

@synthesize window,paused;

- (void) removeStartupFlicker {
}

- (void) applicationDidFinishLaunching:(UIApplication*)application {
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
		[CCDirector setDirectorType:kCCDirectorTypeDefault];
	
	CCDirector *director = [CCDirector sharedDirector];
	
	viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
	viewController.wantsFullScreenLayout = YES;
	
	EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
								   pixelFormat:kEAGLColorFormatRGB565	// kEAGLColorFormatRGBA8
								   depthFormat:0						// GL_DEPTH_COMPONENT16_OES
						];
	[glView setMultipleTouchEnabled:YES];
	
	[director setOpenGLView:glView];
	
	// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
	if( ! [director enableRetinaDisplay:YES] )
		CCLOG(@"Retina Display Not supported");

	[director setDeviceOrientation:kCCDeviceOrientationPortrait];
	[director setAnimationInterval:1.0/60];
//	[director setDisplayFPS:YES];

	[viewController setView:glView];
	[window addSubview: viewController.view];
	[window makeKeyAndVisible];

	[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];

	[self removeStartupFlicker];
	
	[[SimpleAudioEngine sharedEngine] setEffectsVolume:1.0];
	
    NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithInt: UIDeviceOrientationPortrait], 
                              OpenFeintSettingDashboardOrientation,@"TapBurst", 
                              OpenFeintSettingShortDisplayName,  
                              [NSNumber numberWithBool:YES],
                              OpenFeintSettingEnablePushNotifications,
                              [NSNumber numberWithBool:NO], 
                              OpenFeintSettingDisableUserGeneratedContent,nil];
	
    OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:nil];//ofDelegate];
	
    [OpenFeint initializeWithProductKey:@"mkUQ5a7n1aDjL0yQPZYag" 
                              andSecret:@"mk7qBgIXhiizR7TAQ8tjUeVCfasL5rQl7xD1X9voEg"
                         andDisplayName:@"TapBurst High Score"
                            andSettings:settings
                           andDelegates:delegates];
	
	[[CCDirector sharedDirector] runWithScene: [SceneLogo node]];
}

- (void)applicationWillResignActive:(UIApplication *)application {
	CCScene* current = [[CCDirector sharedDirector] runningScene];
	if([current isKindOfClass:[SceneMain class]])
	{
		LayerMain* l = (LayerMain*)[current getChildByTag:kLayerMain];
		[l reStart];
	}
	[[CCDirector sharedDirector] pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	[[CCDirector sharedDirector] resume];
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	[[CCDirector sharedDirector] purgeCachedData];
}

-(void) applicationDidEnterBackground:(UIApplication*)application {
	[[CCDirector sharedDirector] stopAnimation];
}

-(void) applicationWillEnterForeground:(UIApplication*)application {
	[[CCDirector sharedDirector] startAnimation];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	CCDirector *director = [CCDirector sharedDirector];
	[[director openGLView] removeFromSuperview];
	[viewController release];
	[window release];
	[director end];	
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
	[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}

+(tapburstAppDelegate*)get {
	return (tapburstAppDelegate*) [[UIApplication sharedApplication] delegate];
}

- (void)dealloc {
	[[CCDirector sharedDirector] release];
	[window release];
	[super dealloc];
}

@end
